<?php
class Device extends CI_Controller{
	public 	function __construct(){
		parent::__construct();
        $this->load->model('administrator/Mddevice');
        $this->load->library('session');
	}
	public function index($offset = 0)
    {
       
        $this->load->library('pagination');
        $config['base_url'] = base_url('index.php/administrator/device/index');
        $config['total_rows'] = $this->Mddevice->record_count();
        $config['per_page'] = 10;
        $config["uri_segment"] = 3;
        $choice = $config["total_rows"] / $config["per_page"];
        $config["num_links"] = round($choice);
        $this->pagination->initialize($config);
        
        
        
        $temp["links"] = $this->pagination->create_links();
        $temp["template"] = "administrator/device/list";
        $temp['total']=$this->Mddevice->record_count();
		$temp["data"] = $this->Mddevice->listDevice($config["per_page"],$offset);
        $this->load->view("administrator/index",$temp);
       
	}
    public function delete($id)
    {
        if(!empty($id)){
            $this->Mddevice->delDevice($id);  
            $this->session->set_flashdata('message', 'Device were successfully deleted!');
        }else{
            $this->session->set_flashdata('message', 'Not found id device!');
        }
         redirect('index.php/administrator/device/index');
     }
     function edit($device_id) {
        $this->load->helper('form');
        $this->load->library('form_validation'); 
        /* call form validate */
        $this->form_validation->set_rules("d_name","d_name", "required");
        $this->form_validation->set_rules("d_imageDir","d_imageDir", "required");
       
        /* check data post recieved*/
       if($this->input->post('ok')){
            /*validate data follow field*/
            if ($this->form_validation->run() == FALSE) 
            {             
                /*validate failed */
                 $temp["template"] = "administrator/device/edit";
        		 $temp["data"] = "Edit infor Device";
                 $temp["dataPost"]=$this->input->post();
    	    	 $this->load->view("administrator/index",$temp);
            } else {  
                /*validate success */
               // send data to model
               $this->Mddevice->editDevice($device_id,$this->input->post()); 
               // redirect to action index
               $this->session->set_flashdata('message', "Update successfully!");
               redirect('index.php/administrator/device/index'); 
            } 
        }else{
            // not post data
             $temp["template"] = "administrator/device/edit";
    		 $temp["data"] = "Edit infor Device";
             // save data when not post
             $temp["dataPost"]=$this->Mddevice->listDeviceId($device_id);
             $this->load->view("administrator/index",$temp);
        }
   	}	
    public function add()
    {
        $this->load->helper('form');
        $this->load->library('form_validation'); 
        /* call form validate */
        $this->form_validation->set_rules("d_name","d_name", "required");
        $this->form_validation->set_rules("d_imageDir","d_imageDir", "required");
        
        /* check data post recieved*/
       if($this->input->post('ok')){
            /*validate data follow field*/
            if ($this->form_validation->run() == FALSE) 
            {             
                /*validate failed */
                 $temp["template"] = "administrator/device/add";
        		 $temp["data"] = "Add new Device";
                 $temp["dataPost"]=$this->input->post();
    	    	 $this->load->view("administrator/index",$temp);
            } else {  
                /*validate success */
               // send data to model
               $this->Mddevice->addDevice($this->input->post()); 
               // redirect to action index
               $this->session->set_flashdata('message', "<p>Device added successfully.</p>");
               redirect('index.php/administrator/device/index'); 
            } 
        }else{
            // not post data
             $temp["template"] = "administrator/device/add";
    		 $temp["data"] = "Add new Device";
             // save data when not post is array null
             $temp["dataPost"]=array('d_name'=>"","d_imageDir"=>"","d_borderrightHeight"=>'',"d_borderrightWidth"=>"","d_borderTopHeight"=>"",
             "d_borderBottomHeight"=>"","d_width"=>'','d_height'=>"","d_widthL"=>"","d_heightL"=>"","d_topHeight"=>"","d_coverHeight"=>"",
             "d_bottomHeight"=>"","d_shadowHeight"=>"","d_arrowWidth"=>"","d_buttonsHeight"=>"","d_arrowHeight"=>"","d_buttonsHeight"=>"",
             "d_sideButtonsWidth"=>"","d_buttonDLWidth"=>"","d_logoMaxWidth"=>"","d_logoMaxHeight"=>"","d_logoPadding"=>""
             );
	    	 $this->load->view("administrator/index",$temp);
        }
    }
}
?>